package com.jeff.tianti.controller;

import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.common.util.ExcelUtils;
import com.jeff.tianti.org.dto.VolunteerQueryDTO;
import com.jeff.tianti.org.entity.Patient;
import com.jeff.tianti.org.entity.Volunteer;
import com.jeff.tianti.org.service.VolunteerService;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 志愿者和串者入口
 * @author guost
 */
@Controller
@RequestMapping("/volunteer")
public class VolunteerController {
    @Resource
    private VolunteerService volunteerService;

    @RequestMapping(value="/addVolunteer",method = RequestMethod.POST)
    public String addVolunteer(Volunteer volunteer) {
        System.out.println("获得的参数：" + volunteer.getName());
        Volunteer saveVolunteer = volunteerService.save(volunteer);
        System.out.println("保存后的ID=" + saveVolunteer.getId());
        return "redirect:/success";
    }

    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model){
        String userName = request.getParameter("userName");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        int currentPage = 1;
        int pageSize = 10;
        if(StringUtils.isNotBlank(currentPageStr)){
            currentPage = Integer.parseInt(currentPageStr);
        }
        if(StringUtils.isNotBlank(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        VolunteerQueryDTO volunteerQueryDTO = new VolunteerQueryDTO();
        volunteerQueryDTO.setName(userName);
        volunteerQueryDTO.setCurrentPage(currentPage);
        volunteerQueryDTO.setPageSize(pageSize);

        PageModel<Volunteer> page = volunteerService.queryVolunteerPage(volunteerQueryDTO);
        model.addAttribute("page", page);
        model.addAttribute("volunteerQueryDTO", volunteerQueryDTO);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_VOLUNTEER_LIST);

        return "volunteer/volunteer_list";
    }

    /**
     * 导出志愿者数据
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/export")
    public String exportOrder(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("userName");
        Map<String, Object> params = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(name)) {
            params.put("username", name);
        }

        List<Volunteer> volunteerList = volunteerService.getAll();
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String, String> headNameMap = new LinkedHashMap<String, String>();
        headNameMap.put("name", "名称");
        headNameMap.put("sex", "姓别");
        headNameMap.put("identityCard", "身份证号");
        headNameMap.put("company", "工作单位/就读学校");
        headNameMap.put("mobile", "联系电话");
        headNameMap.put("email", "Email");
        headNameMap.put("wechat", "微信");
        headNameMap.put("workExperience", "工作经历");
        headNameMap.put("speciality","特长方向");
        headNameMap.put("createDate","创建时间");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(volunteerList != null && volunteerList.size() > 0){
            for(Volunteer volunteer : volunteerList){
                String createDate = "";

                if(volunteer.getCreateDate() != null){
                    createDate = sdf.format(volunteer.getCreateDate());
                }

                Map<String,Object> map = new HashMap<String,Object>();
                map.put("name", volunteer.getName());
                map.put("sex", volunteer.getSex());
                map.put("identityCard", volunteer.getIdentityCard());
                map.put("company", volunteer.getCompany());
                map.put("mobile", volunteer.getMobile());
                map.put("email", volunteer.getEmail());
                map.put("wechat", volunteer.getWechat());
                map.put("workExperience", volunteer.getWorkExperience());
                map.put("speciality",volunteer.getSpeciality());
                map.put("createDate",createDate);

                list.add(map);
            }
        }

        ExcelUtils.exportXlsx(response, "志愿者数据", headNameMap, list);
        return null;
    }
}
